<template>
<el-dialog
    :title="$t('auth.new_admin')"
    v-model="dialogVisible"
    @close="dialogFormVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <!--form表单-->
    <el-form
      size="small"
      ref="form"
      :model="form"
      :rules="formRules"
      :label-width="formLabelWidth"
    >
      <el-form-item :label="$t('auth.username')" prop="userName"
        ><el-input v-model="form.userName" :placeholder="$t('auth.input_username')"></el-input
      ></el-form-item>
      <el-form-item :label="$t('auth.assigned_role')" prop="roleId">
        <el-select v-model="form.roleId" :multiple="true">
          <el-option
            v-for="item in roleList"
            :value="item.roleId"
            :key="item.roleId"
            :label="item.roleName"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('auth.login_password')" prop="password">
        <el-input
          v-model="form.password"
          :placeholder="$t('auth.input_login_password')"
          type="password"
        ></el-input>
      </el-form-item>
      <el-form-item :label="$t('auth.confirm_password')" prop="confirmPassword">
        <el-input
          v-model="form.confirmPassword"
          :placeholder="$t('auth.input_confirm_password')"
          type="password"
        ></el-input>
      </el-form-item>
      <el-form-item :label="$t('auth.name')" prop="realName"
        ><el-input v-model="form.realName"></el-input
      ></el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">{{$t('auth.cancel_button')}}</el-button>
        <el-button type="primary" @click="onSubmit" :loading="loading"
          >{{$t('auth.confirm_button')}}</el-button
        >
      </div>
    </template>
  </el-dialog>
</template>

<script>
import AuthApi from "@/api/auth.js";
export default {
  data() {
    return {
      /*左边长度*/
      formLabelWidth: "120px",
      /*是否显示*/
      loading: false,
      /*是否显示*/
      dialogVisible: false,
      /*form表单对象*/
      form: {
        userName: ""
      },
      /*form验证*/
      formRules: {
        userName: [{
          required: true,
          message: " ",
          trigger: "blur"
        }],
        roleId: [{
          required: true,
          message: " ",
          trigger: "blur"
        }],
        password: [{
          required: true,
          message: " ",
          trigger: "blur"
        }],
        confirmPassword: [{
          required: true,
          message: " ",
          trigger: "blur"
        }],
        realName: [{
          required: true,
          message: " ",
          trigger: "blur"
        }]
      }
    };
  },
  props: ["open", "roleList"],
  watch: {
    open: function (n, o) {
      if (n != o) {
        this.dialogVisible = this.open;
      }
    }
  },
  created() {},
  methods: {
    /*添加*/
    onSubmit() {
      let self = this;
      self.loading = true;
      let params = self.form;
      AuthApi.userAdd(params, true).then(data => {
        self.loading = false;
        ElMessage({
          message: $t("auth.add_admin_success"),
          type: "success"
        });
        self.dialogFormVisible(true);
      }).catch(error => {
        self.loading = false;
      });
    },
    /*关闭弹窗*/
    dialogFormVisible(e) {
      if (e) {
        this.$emit("close", {
          type: "success",
          openDialog: false
        });
      } else {
        this.$emit("close", {
          type: "error",
          openDialog: false
        });
      }
    }
  }
};
</script>

